package com.taptech.doufu.ui.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.bean.MineNovelBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.services.NovelCreateServices;
import com.taptech.doufu.ui.activity.BrowseActivity;
import com.taptech.doufu.util.UIUtil;
import com.taptech.doufu.net.httputils.HttpResponseObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by op0509 on 2016/5/17.
 */
public class NovelAptitudeDialog extends Dialog implements View.OnClickListener, HttpResponseListener {
    private Context context;
    private LinearLayout llBL, llGL, llBG, llGB, llNULL;
    private TextView tvBL, tvGL, tvBG, tvGB, tvNULL,tvTip;
    private ImageView imgBL, imgGL, imgBG, imgGB, imgNULL, imgHelper;
    private View viewFirst, viewSecond, viewThird;
    private EditText etFirstA, etFirstB, etSecondA, etSecondB, etThirdNameA, etThirdNameB;
    private Button btnCancel, btnSure;
    private MineNovelBean bean;
    private int from, typeAptitude;
    private String nameA, nameB, novelType;
    private WaitDialog waitDialog;
    private AptitudeTypeSelectListener listener;
    private List<Integer> sex;

    private boolean hasSetTag;  //如果已经设置了标签，并且选项改动后则给出提示
    private int oldTypeAltitude;//记录旧的选项，当选项改变时给出提示

    public interface AptitudeTypeSelectListener{
        void selectAptitudeTwoListener(String typeAptitudeOne, String typeAptitudeTwo, int typeNum);
    }

    public void setAptitudeTypeSeleceListener(AptitudeTypeSelectListener listener){
        this.listener = listener;
    }

    public NovelAptitudeDialog(Context context) {
        super(context);
        this.context = context;
    }



    public NovelAptitudeDialog(Context context, int from, int theme,boolean hasSetTag,
                               int typeAptitude, String nameA, String nameB, MineNovelBean bean) {
        super(context, theme);
        this.hasSetTag = hasSetTag;
        this.context = context;
        this.from = from;// 表示从哪个页面过来，2为文评页面，1为小说创建页面, 3为小说设置页面
        this.typeAptitude = typeAptitude;
        this.oldTypeAltitude = this.typeAptitude;
        this.nameA = nameA;
        this.nameB = nameB;
        this.bean = bean;
        waitDialog = new WaitDialog(context, R.style.updateDialog, "处理中,请稍后...");
        if(this.bean != null){
            novelType = bean.getNovel_type();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_novel_create_aptitude);

        sex = new ArrayList<>();
        initUI();
        setProtagonistVisibility(typeAptitude);
        selectAptitude(typeAptitude);
        if(!TextUtils.isEmpty(nameA) && !TextUtils.isEmpty(nameB)){
            setName(typeAptitude, nameA, nameB);
        }
        NovelCreateServices.getInstance().getSexuality(this);// 需求不先确定好，先说本地写死，后面叫后台出接口也不说，接口出来也没告知，目前就本地写死，性向的type值后台取
    }

    private void initUI() {
        tvTip = (TextView) findViewById(R.id.tvTip);
        llBL = (LinearLayout) findViewById(R.id.ll_dialog_novel_create_aptitude_bl);
        llBL.setOnClickListener(this);
        tvBL = (TextView) findViewById(R.id.tv_dialog_novel_aptitude_bl);
        imgBL = (ImageView) findViewById(R.id.img_dialog_novel_aptitude_bl);

        llBG = (LinearLayout) findViewById(R.id.ll_dialog_novel_create_aptitude_bg);
        llBG.setOnClickListener(this);
        tvBG = (TextView) findViewById(R.id.tv_dialog_novel_aptitude_bg);
        imgBG = (ImageView) findViewById(R.id.img_dialog_novel_aptitude_bg);

        llGB = (LinearLayout) findViewById(R.id.ll_dialog_novel_create_aptitude_gb);
        llGB.setOnClickListener(this);
        tvGB = (TextView) findViewById(R.id.tv_dialog_novel_aptitude_gb);
        imgGB = (ImageView) findViewById(R.id.img_dialog_novel_aptitude_gb);

        llGL = (LinearLayout) findViewById(R.id.ll_dialog_novel_create_aptitude_gl);
        llGL.setOnClickListener(this);
        tvGL = (TextView) findViewById(R.id.tv_dialog_novel_aptitude_gl);
        imgGL = (ImageView) findViewById(R.id.img_dialog_novel_aptitude_gl);

        llNULL = (LinearLayout) findViewById(R.id.ll_dialog_novel_create_aptitude_null);
        llNULL.setOnClickListener(this);
        tvNULL = (TextView) findViewById(R.id.tv_dialog_novel_aptitude_null);
        imgNULL = (ImageView) findViewById(R.id.img_dialog_novel_aptitude_null);

        viewFirst = findViewById(R.id.include_dialog_novel_aptitude_first);
        etFirstA = (EditText) viewFirst.findViewById(R.id.et_viewstub_two_text_a);
        etFirstB = (EditText) viewFirst.findViewById(R.id.et_viewstub_two_text_b);

        viewSecond = findViewById(R.id.include_dialog_novel_aptitude_second);
        etSecondA = (EditText) viewSecond.findViewById(R.id.et_viewstub_two_text_a);
        etSecondB = (EditText) viewSecond.findViewById(R.id.et_viewstub_two_text_b);

        viewThird = findViewById(R.id.include_dialog_novel_aptitude_third);
        etThirdNameA = (EditText) viewThird.findViewById(R.id.et_viewstub_two_text_a);
        etThirdNameB = (EditText) viewThird.findViewById(R.id.et_viewstub_two_text_b);

        btnCancel = (Button) findViewById(R.id.btn_dialog_novel_create_publish_cancel);
        btnCancel.setOnClickListener(this);
        btnSure = (Button) findViewById(R.id.btn_dialog_novel_create_publish_sure);
        btnSure.setOnClickListener(this);

        imgHelper = (ImageView) findViewById(R.id.img_dialog_novel_create_aptitude_help);
        imgHelper.setOnClickListener(this);
    }


    private void setTip(int currAptitude) {
        if (oldTypeAltitude != currAptitude && hasSetTag) {
            tvTip.setVisibility(View.VISIBLE);
        }else{
            tvTip.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        int num;
        switch (v.getId()){
            case R.id.ll_dialog_novel_create_aptitude_bl:
                etFirstA.setHint("攻全名(选填)");
                etFirstB.setHint("受全名(选填)");
                if(sex != null && sex.size() > 0){
                    num = sex.get(0);
                }else {
                    num = 1;
                }
                setProtagonistVisibility(num);
                selectAptitude(num);
                setTip(num);
                break;
            case R.id.ll_dialog_novel_create_aptitude_gl:
                etFirstA.setHint("主角A全名(选填)");
                etFirstB.setHint("主角B全名(选填)");
                if(sex != null && sex.size() > 1){
                    num = sex.get(1);
                }else {
                    num = 2;
                }
                setProtagonistVisibility(num);
                selectAptitude(num);
                setTip(num);
                break;
            case R.id.ll_dialog_novel_create_aptitude_bg:
                if(sex != null && sex.size() > 2){
                    num = sex.get(2);
                }else {
                    num = 3;
                }
                setProtagonistVisibility(num);
                selectAptitude(num);
                setTip(num);
                break;
            case R.id.ll_dialog_novel_create_aptitude_gb:
                if(sex != null && sex.size() > 3){
                    num = sex.get(3);
                }else {
                    num = 4;
                }
                setProtagonistVisibility(num);
                selectAptitude(num);
                setTip(num);
                break;
            case R.id.ll_dialog_novel_create_aptitude_null:
                if(sex != null && sex.size() > 4){
                    num = sex.get(4);
                }else {
                    num = 5;
                }
                setProtagonistVisibility(num);
                selectAptitude(num);
                setTip(num);
                break;
            case R.id.btn_dialog_novel_create_publish_sure:
                if(typeAptitude == 1 || typeAptitude == 2){
                    nameA = etFirstA.getText().toString();
                    nameB = etFirstB.getText().toString();
                }else if (typeAptitude == 3 || typeAptitude == 4){
                    nameA = etSecondA.getText().toString();
                    nameB = etSecondB.getText().toString();
                }else if(typeAptitude == 5){
                    nameA = etThirdNameA.getText().toString();
                    nameB = etThirdNameB.getText().toString();
                }else{
                    UIUtil.toastMessage(context, "请选择性向");
                    return;
                }
                if(3 == from){
                    if(waitDialog != null && !waitDialog.isShowing()){
                        waitDialog.show();
                    }
                    String id = "";
                    if (bean != null) {
                        id = bean.getId();
                    }
                    NovelCreateServices.getInstance().saveTopicNovel(this, id, novelType, null, null, null, typeAptitude+"", nameA, nameB);
                }else {
                    listener.selectAptitudeTwoListener(nameA, nameB, typeAptitude);
                    dismiss();
                }
                break;
            case R.id.btn_dialog_novel_create_publish_cancel:
                dismiss();
                break;
            case R.id.img_dialog_novel_create_aptitude_help:
                if(2 == from){
                    BrowseActivity.startActivity(context, "http://api.doufu.la/index.php/journal/detail?id=68");
                }else{
                    BrowseActivity.startActivity(context, "http://api.doufu.la/index.php/journal/detail?id=63");
                }
                break;
            default:
                break;
        }
    }

    private void setProtagonistVisibility(int type){
        switch (type){
            case 1:
            case 2:
                setProtagonistName(viewFirst, viewSecond, viewThird);
                break;
            case 3:
            case 4:
                setProtagonistName(viewSecond, viewFirst, viewThird);
                break;
            case 5:
                setProtagonistName(viewThird, viewFirst, viewSecond);
                break;
            default:
                break;
        }
    }

    private void setProtagonistName(View visiblityFirst, View goneOne, View goneTwo) {
        if(goneOne.getVisibility() == View.VISIBLE){
            goneOne.setVisibility(View.GONE);
        }
        if(goneTwo.getVisibility() == View.VISIBLE){
            goneTwo.setVisibility(View.GONE);
        }
        if(visiblityFirst.getVisibility() != View.VISIBLE){
            visiblityFirst.setVisibility(View.VISIBLE);
        }
    }

    private void selectAptitude(int type){
        tvBL.setTextColor(context.getResources().getColor(R.color.novel_create_text));
        tvGL.setTextColor(context.getResources().getColor(R.color.novel_create_text));
        tvBG.setTextColor(context.getResources().getColor(R.color.novel_create_text));
        tvGB.setTextColor(context.getResources().getColor(R.color.novel_create_text));
        tvNULL.setTextColor(context.getResources().getColor(R.color.novel_create_text));
        imgBL.setVisibility(View.INVISIBLE);
        imgGL.setVisibility(View.INVISIBLE);
        imgBG.setVisibility(View.INVISIBLE);
        imgGB.setVisibility(View.INVISIBLE);
        imgNULL.setVisibility(View.INVISIBLE);
        typeAptitude = type;
        switch (type){
            case 1:
                tvBL.setTextColor(context.getResources().getColor(R.color.cfe6e6e));
                imgBL.setVisibility(View.VISIBLE);
                break;
            case 2:
                tvGL.setTextColor(context.getResources().getColor(R.color.cfe6e6e));
                imgGL.setVisibility(View.VISIBLE);
                break;
            case 3:
                tvBG.setTextColor(context.getResources().getColor(R.color.cfe6e6e));
                imgBG.setVisibility(View.VISIBLE);
                break;
            case 4:
                tvGB.setTextColor(context.getResources().getColor(R.color.cfe6e6e));
                imgGB.setVisibility(View.VISIBLE);
                break;
            case 5:
                tvNULL.setTextColor(context.getResources().getColor(R.color.cfe6e6e));
                imgNULL.setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }
    }

    private void setName(int type, String nameA, String nameB){
        if(type == 1 || type == 2){
            etFirstA.setText(nameA);
            etFirstB.setText(nameB);
            etFirstA.requestFocus();
            etFirstB.requestFocus();
        }else if(type == 3 || type == 4){
            etSecondA.setText(nameA);
            etSecondB.setText(nameB);
            etSecondA.requestFocus();
            etSecondB.requestFocus();
        }else if(type == 5){
            etThirdNameA.setText(nameA);
            etThirdNameB.setText(nameB);
            etThirdNameA.requestFocus();
            etThirdNameB.requestFocus();
        }
    }

    @Override
    public void handleResponse(int handleType, HttpResponseObject response) {
        if(waitDialog != null && waitDialog.isShowing()){
            waitDialog.dismiss();
        }
        if(handleType == NovelCreateServices.HANDLE_TYPE_GET_SEXUALITY){
            if(response.getStatus() == 0){
                JSONObject data = (JSONObject) response.getData();
                try {
                    JSONArray array = data.getJSONArray("list");
                    if(array != null && array.length() != 0){
                        for(int i = 0 ; i < array.length() ; i++){
                            sex.add(array.getJSONObject(i).getInt("sexuality"));
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }else {
            try {
                if(response.getStatus() == Constant.STATUS_SUCCESS){
                    UIUtil.toastMessage(context, "修改成功");
                    listener.selectAptitudeTwoListener(nameA, nameB, typeAptitude);
                    dismiss();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
